<?php
/*{{{LICENSE
+-----------------------------------------------------------------------+
| blogsender                                                            |
+-----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify  |
| it under the terms of the GNU General Public License as published by  |
| the Free Software Foundation. You should have received a copy of the  |
| GNU General Public License along with this program.  If not, see      |
| http://www.gnu.org/licenses/.                                         |
| Copyright (C) 2008-2009. All Rights Reserved.                         |
+-----------------------------------------------------------------------+
| Supports: http://code.google.com/p/blogsender/                        |
+-----------------------------------------------------------------------+
}}}*/

/**
 * @package blogsender
 */

class sohu extends base{
	var $loginUrl = "https://passport.sohu.com/sso/login.jsp";
	var $postUrl ="http://blog.sohu.com/manage/entry.do";
	var $postInitUrl = "http://blog.sohu.com/manage/entry.do?m=add";
	
	function login($username,$password){

		$loginData =array();
		$loginData['userid'] = $username;
		$loginData['password'] = md5($password);
		$loginData['appid'] = "1019";
		$loginData['persistentcookie'] = "0";
		$loginData['s'] = time();
		$loginData['b'] = "1";
		$loginData['w'] = 1440;
		$loginData['pwdtype'] = 1;
		$httpResponse = sender::get($this->loginUrl,$loginData);
		if(stripos($httpResponse['result'],"success")!==false){
			return true;
		} 
		return false;
	}
	
	function _postInit(){
        $httpResponse = sender::get($this->postInitUrl);
		
        preg_match_all ("/\<input(.+?)\>/i", $httpResponse['result'], $_matches,PREG_SET_ORDER);
        $inputs=array();
        foreach($_matches as $_matche){
            preg_match("/name\=\"(.+?)\"/i",$_matche[0],$_name);
            preg_match("/value\=\"(.*?)\"/i",$_matche[0],$_value);
			if(empty($_name[1]))continue;
            $inputs[$_name[1]] = @$_value[1];
        }
        return $inputs;
    } 
	
	function post($content){
		$inputs = $this->_postInit();
		foreach($content as $k=>$v){
			$inputs[$k]=$v;
		}
		$httpResponse = sender::post($this->postUrl ,$inputs);
		if(stripos(implode("\n",$httpResponse['header']),"msg")){
			return true;
		}return false;
	}
	function getContent($title,$text,$tags=""){
		$content = new stdclass;
		$content->categoryId = 0;
		$content->entrycontent = $text;
		$content->entrytitle	=$title;
		$content->excerpt		="";
		$content->keywords		=$tags;
		return $content;
	}

}